package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getThreadId());
        qw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }
    //购物车添加物品
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart:{}", shoppingCart);
        //注意:如果是添加的同一道菜品或套餐,那么更改对应菜品/套餐number+1
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(shoppingCart.getDishId() !=null, ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null, ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        List<ShoppingCart> cartList = shoppingCartService.list(qw);
        log.info("cartList是否为null:{}", cartList);

        if(!cartList.isEmpty()){
            //存在,修改number字段+1
            ShoppingCart shoppingCartZero = cartList.get(0);
            log.info("number数量的值+1前:{}",shoppingCartZero.getNumber());
            shoppingCartZero.setNumber(shoppingCartZero.getNumber()+1);
            log.info("number数量的值+1后:{}",shoppingCartZero.getNumber());
            shoppingCartService.update(shoppingCartZero, qw);
            return R.success(shoppingCartZero);
        }else {
            //不存在,保存到表中
            shoppingCart.setUserId(BaseContext.getThreadId());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }

    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContext.getThreadId());
        shoppingCartService.remove(qw);
        return R.success("删除成功");
    }

    //购物车货物数量-1
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody Map<String,Object> map){
        log.info("map:{}", map);
        String dishId =(String) map.get("dishId");  //菜品id
        String setmealId =(String) map.get("setmealId");  //菜品id
        //修改number字段-1
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        qw.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        List<ShoppingCart> list = shoppingCartService.list(qw);

        ShoppingCart shoppingCart = list.get(0);

        if (shoppingCart.getNumber() ==1){
            shoppingCartService.remove(qw);
            R<ShoppingCart> newR = new R<>();
            ShoppingCart data = new ShoppingCart();
            newR.setCode(1);
            data.setNumber(0);
            newR.setData(data);
            return newR;

        }else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartService.update(shoppingCart,qw);
            return R.success(shoppingCart);
        }

    }

}
